//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.MaterialOutListController');
	com.yonyou.ncmm_pac_pda.MaterialOutListController = function() {
		com.yonyou.ncmm_pac_pda.MaterialOutListController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$MaterialOutListController$initialize() {

	}

	function com$yonyou$ncmm_pac_pda$MaterialOutListController$evaljs(js) {
		eval(js)
	}

	//返回按钮单击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$button0_onclick(sender, args) {
		// 返回即关闭当前页面
		$view.close();
	}

	//公共菜单按钮单击事件
	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("btn_menu", true);
	}

	//公共菜单-扫描记录按钮单击事件
	function showScanList() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.MaterialOutScanList", //目标页面（首字母大写）全名
			"isKeep": "true"
		});
	}

	//页面重新启动的时候调用
	function pageRestart(sender, args) {
		showDataFromLocalDB();
	}

	//"今天"按钮单击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$button1_onclick(sender, args) {
		// 按照默认查询条件查询当天生产订单数据
		var condition = getDefaultCondition();
		if(condition == null)
			return;

		var billndate = "between '" + (NCMMDateUtil.getDayDate())["beginDate"] + "' and '" + (NCMMDateUtil.getDayDate())["endDate"] + "'";
		condition[MaterialOutVO.DBILLDATE.name] = billndate;
		//获取单据数据并显示
		//		$alert("condition=" + $jsonToString(condition));
		//		loadBillDataFromNC(condition);
		NCMMMOUtil.processBillDataFromNC(MaterialOutVO, "材料出库单", condition, function(data) {
			if(data) {
				$toast("材料出库单下载了【" + data + "】条数据。");
				//从本地数据库读取数据并显示在界面上
				showDataFromLocalDB();
			}
		});
	}

	//"本周"按钮单击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$button2_onclick(sender, args) {
		// 默认查询本周生产订单数据
		var condition = getDefaultCondition();
		if(condition == null)
			return;

		var daydate = NCMMDateUtil.getWeekDate();
		var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
		condition[MaterialOutVO.DBILLDATE.name] = dbilldate;
		//获取单据数据并显示
		//		loadBillDataFromNC(condition);
		NCMMMOUtil.processBillDataFromNC(MaterialOutVO, "材料出库单", condition, function(data) {
			if(data) {
				$toast("材料出库单下载了【" + data + "】条数据。");
				//从本地数据库读取数据并显示在界面上
				showDataFromLocalDB();
			}
		});
	}

	//"本月"按钮单击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$button3_onclick(sender, args) {
		// 默认查询本月生产订单数据
		var condition = getDefaultCondition();
		if(condition == null)
			return;

		var daydate = NCMMDateUtil.getMonthDate();
		var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
		condition[MaterialOutVO.DBILLDATE.name] = dbilldate;
		//获取单据数据并显示
		//loadBillDataFromNC(condition);
		NCMMMOUtil.processBillDataFromNC(MaterialOutVO, "材料出库单", condition, function(data) {
			if(data) {
				$toast("材料出库单下载了【" + data + "】条数据。");
				//从本地数据库读取数据并显示在界面上
				showDataFromLocalDB();
			}
		});
	}

	//订单过滤条件：
	//stockcode=默认库存组织编码
	//storcode=默认仓库编码
	//如果没有设置默认库存或默认仓库，不能下载单据，给出提示
	function getDefaultCondition() {
		var stockorg = $cache.read("defaultstockorg") ? $cache.read("defaultstockorg") : "";
		if(stockorg != "") {
			stockorg = $stringToJSON(stockorg);
			stockorg = stockorg["pk_stockorg"];
		}
		var stordoc = $cache.read("defaultstordoc") ? $cache.read("defaultstordoc") : "";
		if(stordoc != "") {
			stordoc = $stringToJSON(stordoc);
			stordoc = stordoc["pk_stordoc"];
		}
		if(stockorg == "" || stordoc == "") {
			$toast("下载单据前，请先进行业务设置");
			return null;
		}

		var condition = {};
		condition[MaterialOutVO.PKORGH.name] = "='" + stockorg + "'";
		condition[MaterialOutVO.PKSTORDOC.name] = "='" + stordoc + "'";
		return condition;
	}

	//从NC端获取单据数据、保存至本地数据库、显示在界面上
	//先删除旧的
	//插入新的
	//对照扫描记录更新扫描数量
	//	function loadBillDataFromNC(condition) {
	//		$js.showLoadingBar({
	//			msg: "正在连接服务器..."
	//		})
	//		var host = $cache.read('host');
	//		var port = $cache.read('port');
	//		$app.checkNetWorkReachable(host, port, function(param, args) {
	//			if(args.is_reachable == 'true') {
	//				//$js.toast("服务器地址连接正常！");
	//				$js.showLoadingBar({
	//					msg: "正在下载..."
	//				})
	//				NCMMDBUtil.loadNCBaseData(MaterialOutVO, MaterialOutVO, NCMMHttpUtil.BILL, condition, function(data) {
	//					//如果没有从NC端下载到数据，则本地记录不更新
	//					if(data == null) {
	//						$js.hideLoadingBar();
	//						$toast("没有获取到符合条件的订单");
	//						return;
	//					}
	//					if(data != null && typeof(data) == 'string') {
	//						$js.hideLoadingBar();
	//						$toast("下载失败：" + data);
	//						return;
	//					}
	//
	//					// 把NC数据拉平
	//					//					$alert("before strech jsondata="+$jsonToString(data.billinfos));
	//					var jsondata = parseNCData2Local(data.billinfos);
	//					//					$alert("jsondata="+$jsonToString(jsondata));
	//					//清空本地表记录
	//					NCMMDBUtil.clearAllOldData(MaterialOutVO, null);
	//					var vocount = NCMMDBUtil.addNewDatas(MaterialOutVO, null, {
	//						"billinfos": jsondata
	//					});
	//					$js.hideLoadingBar();
	//					$toast("材料出库单下载了【" + vocount + "】条数据。");
	//					//从本地数据库读取数据并显示在界面上
	//					showDataFromLocalDB();
	//				});
	//			} else {
	//				$js.hideLoadingBar();
	//				$js.toast("下载失败请检查网络设置！");
	//			}
	//
	//		});
	//
	//	}
	//
	//	//拉平NC数据
	//	function parseNCData2Local(data) {
	//		var newData = new Array();
	//
	//		for(var i = 0; i < data.length; i++) {
	//			var dataItem = data[i];
	//			var head = dataItem["billinfo"]["head"];
	//			head = eval(head);
	//			var dataSubItems = dataItem["billinfo"]["items"];
	//			dataSubItems = eval(dataSubItems);
	//
	//			for(var j = 0; j < dataSubItems.length; j++) {
	//				var item = dataSubItems[j]["item"];
	//				item = eval(item);
	//				//把两个json对象合并成一个新的json对象
	//				var newDataItem = eval('(' + (JSON.stringify(head) + JSON.stringify(item)).replace(/}{/, ',') + ')');
	//
	//				//获取本地扫码数量
	//				newDataItem[MaterialOutVO.SCANNUM.name] = getLocalScannum(newDataItem[MaterialOutVO.VBILLCODE.name], newDataItem[MaterialOutVO.CROWNO.name]);
	//				//				$alert($jsonToString({
	//				//					SCANNUM: newDataItem[MaterialOutVO.SCANNUM.name]
	//				//				}));
	//				newData.push(newDataItem);
	//			}
	//		}
	//		return newData;
	//	}

	function processLocalScannum(billinfo) {
		billinfo[MaterialOutVO.SCANNUM.name] = getLocalScannum(billinfo[MaterialOutVO.VBILLCODE.name], billinfo[MaterialOutVO.CROWNO.name]);
		return billinfo;
	}

	//根据单据号和物料行号获得本地的扫码记录
	function getLocalScannum(vbillcode, rowno) {
		var scannum = "";
		//判断扫描记录表是否存在
		if(NCMMPubUtil.isTableExist(MaterialOutScansVO) == false) {
			//			$alert("scantable isnot exist");
			return scannum;
		}
		//where vbillcode = 'vbillcode' and rowno = 'rowno'
		var wheresql = "where " + MaterialOutScansVO.VBILLCODE.name + " = '" + vbillcode + "' and " + MaterialOutScansVO.CROWNO.name + " = '" + rowno + "'";
		var retData = NCMMPubUtil.queryVONoCallback(MaterialOutScansVO, wheresql);
		if(retData == null || retData == "") {
			//			$alert("scantable no record-"+wheresql);
			return scannum;
		}
		retData = eval(retData);
		var col = MaterialOutScansVO.SCANNUM.name;
		for(var i = 0; i < retData.length; i++) {
			var num = retData[i][col];
			if(num == null || num == "")
				continue;
			num = parseFloat(num);
			if(scannum == "")
				scannum = num;
			else {
				scannum = scannum + num;
			}
		}
		return scannum;
	}

	//从本地数据库表中读取数据并显示在界面上
	function showDataFromLocalDB() {
		if(NCMMPubUtil.isTableExist(MaterialOutVO) == false) {
			return;
		}
		//按单据号+行号为序，升序排列
		var wheresql = "order by " + MaterialOutVO.VBILLCODE.name + ", " + MaterialOutVO.CROWNO.name;
		var retData = NCMMPubUtil.queryVONoCallback(MaterialOutVO, wheresql);
		if(retData == null || retData == "") {
			$ctx.push({});
			$toast("没有可显示的订单记录");
			return;
		}
//		$alert("retData1="+$jsonToString(retData));
//		retData = eval(retData);
//		$alert($jsonToString({
//			beforeorders: retData
//		}));
		retData = NCMMPubUtil.htmlspecialjsonarray2UI(retData);
//		$alert("retData2="+$jsonToString(retData));
		
		//根据计量单位转换数量属性的小数位数
		retData = NCMMMOUtil.changeDecimalFiguresByMeascdoc(MaterialOutVO, retData);
		$ctx.push({
			"orders": retData
		});
//		$alert($jsonToString({
//			display: retData
//		}));

		//快速查询设置
		//过滤条件：单据号、物料行号
		NCMMPubUtil.notifyFilter("search0", "listviewdefine0", [MaterialOutScansVO.VBILLCODE.name,
			MaterialOutScansVO.CINVNAME.name,
			MaterialOutScansVO.NSHOULDINNUM.name,
			MaterialOutScansVO.NSHOULDASSISTNUM.name,
			MaterialOutScansVO.SCANNUM.name
		]);
	}

	//快速查询框的onsearch事件
	function selectData(sender, args) {
		// 按照查询条件进行快速查询
		NCMMPubUtil.quickFilter();
	}
	
	//单据码的onchange事件
	function scanonchange(sender, args) {
	    afterScanCode();
	    $ctx.put("scancode", "");
		$ctx.dataBind();
	}

	//查询条件按钮点击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$imagebutton2_onclick(sender, args) {
		//先组装默认查询条件，如果没有进行业务设置，不可以查询
		var defaultcondition = getDefaultCondition();
		if(defaultcondition == null)
			return;
		// 点击查询按钮弹出查询条件画面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.MaterialOutQueryCon",
			isKeep: "true",
			callback: function() {
				var extendscondition = $param.getJSONObject("data");
//				extendscondition = eval(extendscondition);
				//				$alert("extendscondition="+$jsonToString(extendscondition));
				var condtion = {};
				if(extendscondition == null)
					condition = defaultcondition;
				else
					condition = eval('(' + (JSON.stringify(defaultcondition) + JSON.stringify(extendscondition)).replace(/}{/, ',') + ')');

				//根据查询条件下载NC数据到本地表并显示		
				//				loadBillDataFromNC(condition);
				NCMMMOUtil.processBillDataFromNC(MaterialOutVO, "材料出库单", condition, function(data) {
					if(data) {
						$toast("材料出库单下载了【" + data + "】条数据。");
						//从本地数据库读取数据并显示在界面上
						showDataFromLocalDB();
					}
				});
			}
		});
	}

	//扫（单据）码按钮单击事件
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$imagebutton1_onclick(sender, args) {
		$scanner.open({
			bindfield: "scancode",
			callback: "afterScanCode()"
		});
	}

	//扫（单据）码结果处理
	function afterScanCode() {
		// 扫条码调到采集页面
		var scancode = $ctx.getString("scancode");
		var billcode = parsebarcode(scancode);

		//从本地表中根据billcode查找上游单据记录
		var wheresql = "where " + MaterialOutVO.VBILLCODE.name + " = '" + billcode + "'";
		var retData = NCMMPubUtil.queryVONoCallback(MaterialOutVO, wheresql);
		if(retData == null || (eval(retData)).length == 0) {
			$alert("没有单据号为【" + billcode + "】的订单，请重新扫描");
			return;
		}
		retData = eval(retData);
		//只传入单据号
		var headData = getHeadData(retData[0]);
		//扫单据码时候只传递单据号
		var vbillcode = headData.vbillcode;
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.MaterialOutScan",
			isKeep: "true",
			vbillcode: vbillcode,
			callback: function() {
				showDataFromLocalDB();
			}
		});
		//openCollectPage(headData);
		//			
		//		//如果单据对应1行，则直接跳转到采集界面(图2)，传入参数为表记录行所有字段
		//		if(retData.length == 1) {
		//			openCollectPage(retData[0]);
		//		} else {
		//			//如果单据对应多行，则只传入单据号
		//			var headData = getHeadData(retData[0]);
		//			//			alert("headData="+$jsonToString(headData));
		//			openCollectPage(headData);
		//		}
	}

	//经过条码规则解析，解析出单据码
	function parsebarcode(scancode) {
		var billcode = "";
		billcode = scancode;
		return billcode;
	}

	//列表项-采集事件
	function onItemCollect() {
		var row = $id("listviewdefine0").get("row");
		row = $stringToJSON(row);
		row = NCMMPubUtil.htmlspecialjson2UI(row);
//		$alert("row="+$jsonToString(row));

		var bodydata = {};
		bodydata[MaterialOutVO.VBILLCODE.name] = row[MaterialOutVO.VBILLCODE.name];
		bodydata[MaterialOutVO.CROWNO.name] = row[MaterialOutVO.CROWNO.name];

		// 采集打开扫条码页面
		openCollectPage(bodydata);
	}

	//打开采集页面，传入参数为行记录数据
	function openCollectPage(rowData) {
//		$alert("rowData="+$jsonToString(rowData));
		var row = {
			"data": rowData,
			"type": "1" //图1 --> 图3 （采集）
		};
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.MaterialOutScan",
			isKeep: "true",
			row: row,
			callback: function() {
				showDataFromLocalDB();
			}
		});
	}

	//从完整的表记录json中截取表头部分的数据
	function getHeadData(rowData) {
		var headData = {};
		//暂时只带过去单据号
		headData[MaterialOutVO.VBILLCODE.name] = rowData[MaterialOutVO.VBILLCODE.name];
		//		headData[MaterialOutVO.PKHEAD.name] = rowData[MaterialOutVO.PKHEAD.name];
		//		headData[MaterialOutVO.DBILLDATE.name] = rowData[MaterialOutVO.DBILLDATE.name];		
		//		headData[MaterialOutVO.PKSTORDOC.name] = rowData[MaterialOutVO.PKSTORDOC.name];
		//		headData[MaterialOutVO.FBILLFLAG.name] = rowData[MaterialOutVO.FBILLFLAG.name];
		//		headData[MaterialOutVO.CBILLTYPECODE.name] = rowData[MaterialOutVO.CBILLTYPECODE.name];
		//		headData[MaterialOutVO.HTS.name] = rowData[MaterialOutVO.HTS.name];
		//		headData[MaterialOutVO.HDR.name] = rowData[MaterialOutVO.HDR.name];
		//		headData[MaterialOutVO.PKGROUPH.name] = rowData[MaterialOutVO.PKGROUPH.name];
		//		headData[MaterialOutVO.PKORGH.name] = rowData[MaterialOutVO.PKORGH.name];
		//		headData[MaterialOutVO.CDPTID.name] = rowData[MaterialOutVO.CDPTID.name];
		return headData;
	}

	//如果单据对应多行，弹出物料行选择界面，选择要采集的物料行
	function getMaterialRow(billDatas) {
		var materialrows = new Array();
		for(var i = 0; i < billDatas.length; i++) {
			var row = {};
			row["index"] = i;
			row["rowno"] = billDatas[i][MaterialOutVO.CROWNO.name];
			row["code"] = billDatas[i][MaterialOutVO.CINVCODE.name];
			row["name"] = billDatas[i][MaterialOutVO.CINVNAME.name];
			materialrows.push(row);
		}
		//打开物料行选择页面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.MaterialRowRefPage", //目标页面
			isKeep: "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			data: materialrows,
			callback: function() {
				var data = $param.getJSONObject("data");
				if(data == null) {
					$alert("物料行选择有误。");
					return;
				}
				var index = parseInt(data);
				openCollectPage(billDatas[index]);
			}
		});

	}

	// 列表行单击事件 -- 弹出浮动按钮组
	function com$yonyou$ncmm_pac_pda$MaterialOutListController$onitemclick(sender, args) {
		$view.openPop(NCMMMOUtil.popDialogInfo("panel12"));
	}

	//浮动按钮组 -- 详情
	function onclick_Detail() {
		//先关闭按钮组
		onclick_Cancel();

		// 获取当前行内容
		var row = $id("listviewdefine0").get("row");
		//		$alert("row="+row);
		//		row = $stringToJSON(row);
		//		$alert("jsonrow="+$jsonToString(row));
		//		$alert(row[MaterialOutVO.VBILLCODE.name]+"||"+row[MaterialOutVO.CROWNO.name]);
		//		row[MaterialOutVO.DBILLDATE.name] = NCMMDateUtil.getOnlyDate(row[MaterialOutVO.DBILLDATE.name]);

		// 单击行跳转到详情页面
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.MaterialOutDetail",
			"isKeep": "true",
			"row": row
		});
	}
	//浮动按钮组 -- 采集
	function onclick_Scan() {
		//先关闭按钮组
		onclick_Cancel();
		//进入图3（采集界面）		
		onItemCollect();
	}
	//浮动按钮组 -- 删除
	function onclick_Delete() {
		//先关闭按钮组
		onclick_Cancel();
		if($confirm("确定删除所选记录吗？")) {
			//删除列表行
			onItemDelete();
		}
	}
	//浮动按钮组 -- 取消
	function onclick_Cancel() {
		// 取消
		$view.closePop(NCMMMOUtil.popDialogInfo("panel12"));
	}

	//处理list的onload事件
	function onlistload(sender, args) {
		showDataFromLocalDB();

	}

	//列表项-删除事件
	function onItemDelete() {
		var row = $id("listviewdefine0").get("row");
		row = $stringToJSON(row);
		//				$alert($jsonToString({row: row}));
		//扫描数量是否为空
		var scannum = row["scannum"];
		//没有扫描记录-直接删除订单记录， 重新加载界面显示数据
		if(scannum == null || scannum == "") {
			//删除本地单据表记录
			NCMMPubUtil.deleteVO(MaterialOutVO, row);
			//重新加载当前界面
			showDataFromLocalDB();
		} else {
			$alert("该单据有扫描记录，不能删除。");
		}
	}

	function com$yonyou$ncmm_pac_pda$MaterialOutListController$imagebutton3_onclick(sender, args) {

	}

	function com$yonyou$ncmm_pac_pda$MaterialOutListController$checkAll(sender, args) {
		// 全选按钮逻辑
		$ctx.dataCollect();
		var isallchecked = $id(sender).get("checked");
		var orders = $ctx.getJSONArray("orders");
		if(orders == null)
			return;
		for(var i = 0; i < orders.length; i++) {
			orders[i]["ischecked"] = isallchecked;
		}

		var json = {};
		json["orders"] = orders;
		json["isallchecked"] = isallchecked;
		$ctx.push(json);

	}

	function com$yonyou$ncmm_pac_pda$MaterialOutListController$checkSinge(sender, args) {
		// 单选按钮逻辑
		$ctx.dataCollect();
		var ischecked = $id(sender).get("checked");
		var isallchecked = true;
		if(ischecked == false) {
			isallchecked = false;
		} else {
			var orders = $ctx.getJSONArray("orders");
			for(var i = 0; i < orders.length; i++) {
				if(orders[i]["ischecked"] == null || orders[i]["ischecked"] == "false") {
					isallchecked = false;
					break;
				}
			}
		}

		var json = {};
		json["orders"] = orders;
		json["isallchecked"] = isallchecked;
		$ctx.push(json);
	}

	com.yonyou.ncmm_pac_pda.MaterialOutListController.prototype = {
		checkSinge: com$yonyou$ncmm_pac_pda$MaterialOutListController$checkSinge,
		checkAll: com$yonyou$ncmm_pac_pda$MaterialOutListController$checkAll,
		imagebutton3_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$imagebutton3_onclick,
		imagebutton2_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$imagebutton2_onclick,
		imagebutton1_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$imagebutton1_onclick,
		button3_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$button3_onclick,
		button2_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$button2_onclick,
		button1_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$button1_onclick,
		button0_onclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$button0_onclick,
		onitemclick: com$yonyou$ncmm_pac_pda$MaterialOutListController$onitemclick,
		initialize: com$yonyou$ncmm_pac_pda$MaterialOutListController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$MaterialOutListController$evaljs
	};
	com.yonyou.ncmm_pac_pda.MaterialOutListController.registerClass('com.yonyou.ncmm_pac_pda.MaterialOutListController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}